package com.nifostasky.main.annotation.useannotation;

import com.nifostasky.main.annotation.customannotation.BackTransferMoney;
import lombok.SneakyThrows;

import java.lang.reflect.Method;

/**
 * 使用注解处理转账逻辑
 *
 */
public class BankService {
    /**
     * 现在最大金额为15000
     * @param money
     */
    @SneakyThrows
    @BackTransferMoney(maxMoney = 15000) //使用自定义注解,将最大限额修改为15000
    public static void transferMoney(double money){
        String tips = BankService.processAnnotationMoney(money);
        System.out.println(new String(tips.getBytes(),"utf-8"));
    }

    @SneakyThrows
    private static String processAnnotationMoney(double money) {
        //通过反射获取到注解
        Class<BankService> bankServiceClass = BankService.class;
        Method transferMoney = bankServiceClass.getDeclaredMethod("transferMoney", double.class);
        boolean annotationPresent = transferMoney.isAnnotationPresent(BackTransferMoney.class);
        //没有限制金额注解,不限制转账金额,转账成功
        if (!annotationPresent) {
            return "转账成功,转账金额为: " + money;
        }
        BackTransferMoney annotation = transferMoney.getAnnotation(BackTransferMoney.class);
        double maxMoney = annotation.maxMoney();
        if (money > maxMoney) {
            return "转账金额数额太大转账失败,最大支持转账额度为: " + maxMoney;
        } else {
            return "转账成功,转账金额为: " + money;
        }
    }

    public static void main(String[] args) {
        BankService.transferMoney(17000);
    }
}
